//
//  OrderSortMenuView.m
//  SameWorld
//
//  Created by 李小朋 on 2017/3/18.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "OrderSortMenuView.h"
#import "CustomOrderButton.h"
#import "UIColor+RandomColor.h"
#import "UIButton+Extensions.h"
@interface OrderSortMenuView ()

@property (nonatomic ,strong) NSArray *titleArray;
@property (nonatomic ,strong) NSArray *imageArray;
@property (nonatomic ,strong) NSArray *selectedArray;


@property (nonatomic ,strong) UIButton *button;
@property (nonatomic ,strong) UIButton *imageBtn;

// 记录选中的button
@property (nonatomic ,assign) NSInteger lastSelectButtonTag;
@property (nonatomic ,assign) NSInteger lastSelectImageBtnTag;

@end

@implementation OrderSortMenuView

- (instancetype)initWithFrame:(CGRect)frame withTitleArray:(NSArray *)titleArray imageArray:(NSArray *)imageArray selectedImageArray:(NSArray *)selectedImageArray {

    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.titleArray = titleArray;
        self.imageArray = imageArray;
        self.selectedArray = selectedImageArray;
        [self creatBtn];
    }
    return self;
}


- (void)creatBtn {
    
    
    for (int i = 0; i < 5; i ++) {
        UIButton *btn = [self createButtonWithType:(UIButtonTypeCustom) frame:CGRectMake(self.frame.size.width/5 * i, 0, self.frame.size.width/5, 64) title:self.titleArray[i] image:[UIImage imageNamed:[NSString stringWithFormat:@"%@",self.imageArray[i]]] selectedImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",self.selectedArray[i]]] titleColor:[UIColor darkGrayColor] tag:800 + i responseAction:@selector(clickedButton:)];
        if (i == 0) {
            btn.selected = YES;
            [btn setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];

        }
//        btn.backgroundColor = [UIColor randomColor];
        btn.titleLabel.font = [UIFont systemFontOfSize:10];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [btn setImgViewStyle:(ButtonImgViewStyleTop) imageSize:CGSizeMake(22, 22) space:5];
        [self addSubview:btn];
        self.button = btn;
        
        self.lastSelectButtonTag = 800;

    }
    
    
    // 创建底部横线
    UIView *lineView = [[UIView alloc] initWithFrame:(CGRectMake(0,self.frame.size.height - 1, size_width, 1))];
    lineView.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [self addSubview:lineView];
    
    // 创建跟随按钮点击移动的横线
    CGFloat linewww = self.frame.size.width/ 5;
    CGFloat margin = 12;
    CGFloat lineww = linewww - margin*2;
    UIView *selectlineView = [[UIView alloc] initWithFrame:CGRectMake(12, self.frame.size.height - 2, lineww, 2)];
    selectlineView.backgroundColor = [UIColor orangeColor];
    selectlineView.tag = 1111;
    [self addSubview:selectlineView];
}

- (void)clickedButton:(UIButton *)sender {
    
    
    NSInteger tag = sender.tag;
    UIButton *lastSelectButton = [self viewWithTag:self.lastSelectButtonTag];
    [lastSelectButton setTitleColor:[UIColor darkGrayColor] forState:(UIControlStateNormal)];
    lastSelectButton.selected = !lastSelectButton.selected;
        if (lastSelectButton.selected) {
//            NSLog(@"111");
            
        }else{
//            NSLog(@"222");
        }
    
    
    [sender setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    sender.selected = !sender.selected;
        if (sender.selected) {
//            NSLog(@"333");
        }else{
//            NSLog(@"444");
        }

    
    self.lastSelectButtonTag = tag;
    
    UIView *lineView = [self viewWithTag:1111];
    [UIView animateWithDuration:0.5 animations:^{
        lineView.frame = CGRectMake(sender.frame.origin.x + 12, lineView.frame.origin.y, lineView.frame.size.width, lineView.frame.size.height);
    }];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(buttonClickWithTag:)]) {
        [self.delegate buttonClickWithTag:sender.tag];
    }

}



- (UIButton *)createButtonWithType:(UIButtonType)buttonType frame:(CGRect)frame title:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage titleColor:(UIColor *)color tag:(NSInteger)tag responseAction:(SEL)SELname {
    
    UIButton *button = [UIButton buttonWithType:(buttonType)];
    button.frame = frame;
    [button setTitle:title forState:(UIControlStateNormal)];
    [button setTitleColor:color forState:(UIControlStateNormal)];
    [button setImage:image forState:(UIControlStateNormal)];
    [button setImage:selectedImage forState:(UIControlStateSelected)];
    [button setTag:tag];
    [button addTarget:self action:SELname forControlEvents:(UIControlEventTouchUpInside)];
    return button;
}


@end
